package de.gist.calldurationwatcher.preferences;

import java.util.Calendar;
import de.gist.calldurationwatcher.activities.R;
import de.gist.calldurationwatcher.helper.Utils;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.widget.DatePicker;

public class Preferences extends PreferenceActivity {
	
	private SharedPreferences preferences;
	private int mYear;
    private int mMonth;
    private int mDay;
    
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    addPreferencesFromResource(R.xml.preferences);
	    
	    preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
	    
	    final Calendar c = Calendar.getInstance();
        mYear = preferences.getInt(getString(R.string.year_setting), c.get(Calendar.YEAR)); 
        mMonth = preferences.getInt(getString(R.string.month_setting), c.get(Calendar.MONTH));
        mDay = preferences.getInt(getString(R.string.day_setting), c.get(Calendar.DAY_OF_MONTH));
	    
	    Preference contractStartPreference = (Preference) findPreference("contractStartDate");
	    contractStartPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			
			public boolean onPreferenceClick(Preference preference) {
				showDialog(0);
				return true;
			}
		});
	    
	    Preference allNetworksPref = (Preference) findPreference("allNetworks");
	    allNetworksPref.setDefaultValue(false);
	    allNetworksPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			
			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				
				SharedPreferences.Editor editor = preferences.edit();
				boolean allNetworks = (Boolean)newValue;
				if (allNetworks) {
					editor.putBoolean("allNetworks", allNetworks);
					editor.commit();
				}
				
				int minutesLeft = preferences.getInt(getString(R.string.minutesLeft), -1);
				if (minutesLeft == -1)
					return true;
				
				int freeMinutes = Utils.GetCallLogDuration(getApplicationContext());
				if (allNetworks)
					minutesLeft = minutesLeft - freeMinutes;
				else
					minutesLeft = minutesLeft + freeMinutes;
				
				
				editor.putInt(getString(R.string.minutesLeft), minutesLeft);
				editor.commit();
				
				return true;
			}
		});
	}
	
	 @Override
	 protected Dialog onCreateDialog(int id) {
		 
		 if (id == 0) {
			 return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
		 }
		 
		 return null;
	 }
	 
	 @Override
	 protected void onPrepareDialog(int id, Dialog dialog) {
		 
		 ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);   
	 }

	 private DatePickerDialog.OnDateSetListener mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {

                public void onDateSet(DatePicker view, int year, int monthOfYear,
                        int dayOfMonth) {
                    mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                    
                    SharedPreferences.Editor editor = preferences.edit();
                    editor.putInt(getString(R.string.year_setting), mYear);
                    editor.putInt(getString(R.string.month_setting), mMonth);
                    editor.putInt(getString(R.string.day_setting), mDay);
                    editor.commit();
                }
            };
}
